package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.GamePrizesBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.ui.activity.PrizesGameListActivity;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.DownloadViewStrokeV2;

/**
 * Created by Alan on 2018/7/19.
 */

public class MediaTestAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;
    private Map<String, String> downTemp = new HashMap<>();

    public MediaTestAdapter(Activity context, List<ViewBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size()>0 && downTemp.containsKey(gameId)){
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (mDatas.get(itemPo).getViewType() == ViewType.TYPE_GAME) {
            GameListBaseBean tempJump = (GameListBaseBean) mDatas.get(itemPo).getObj();
            return tempJump;
        }
        return null;
    }

    public void clearTempData () {
        downTemp.clear();
    }

    public enum ViewType {
        TYPE_TOP,//头部
        TYPE_GAME//游戏信息
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;
        String jumpModelType;
        boolean isEnd;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType) {
            this.viewType = viewType;
            this.obj = null;
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewBean(ViewType viewType, Object obj, String jumpModelType) {
            this.viewType = viewType;
            this.obj = obj;
            this.jumpModelType = jumpModelType;
        }

        public ViewBean(ViewType viewType, Object obj, String jumpModelType, boolean isEnd) {
            this.viewType = viewType;
            this.obj = obj;
            this.jumpModelType = jumpModelType;
            this.isEnd = isEnd;
        }

        public boolean isEnd() {
            return isEnd;
        }

        public void setEnd(boolean end) {
            isEnd = end;
        }

        public String getJumpModelType() {
            return jumpModelType;
        }

        public void setJumpModelType(String jumpModelType) {
            this.jumpModelType = jumpModelType;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_TOP.ordinal()) {
            holder = new TopHolder(inflater.inflate(R.layout.item_media_test_top_view, parent, false));
        } else if (viewType == ViewType.TYPE_GAME.ordinal()) {
            holder = new GameInfoHolder(inflater.inflate(R.layout.item_media_test_game_view, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof TopHolder) {
            final JumpBaseBean temp = (JumpBaseBean) viewBean.getObj();
            TopHolder topHolder = (TopHolder) holder;

            topHolder.itemMediaTestHistoryNameTv.setText(temp.getJump_sub_title());
            topHolder.itemMediaTestNameTv.setText(temp.getJump_label());
//            topHolder.itemMediaTestHistoryTv.setOnClickListener(new MyListener(mContext, temp));
            topHolder.itemMediaTestHistoryTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, PrizesGameListActivity.class);
                    intent.putExtra("prize_id",temp.getJump_game_prizes_id());
                    mContext.startActivity(intent);
                }
            });

            if (temp.getJump_model_type().equals("1001")) {
                topHolder.itemMediaTestTopLl.setBackgroundResource(R.mipmap.bg_media_test_google);
            } else if (temp.getJump_model_type().equals("1002")) {
                topHolder.itemMediaTestTopLl.setBackgroundResource(R.mipmap.bg_media_test_apple);
            } else if (temp.getJump_model_type().equals("1003")) {
                topHolder.itemMediaTestTopLl.setBackgroundResource(R.mipmap.bg_media_test_tga);
            } else if (temp.getJump_model_type().equals("1004")) {
                topHolder.itemMediaTestTopLl.setBackgroundResource(R.mipmap.bg_media_test_taptap);
            }
        } else if (holder instanceof GameInfoHolder) {
            final GameListBaseBean temp = (GameListBaseBean) viewBean.getObj();
            GameInfoHolder gameInfoHolder = (GameInfoHolder) holder;

            String icoUrl = temp.getGame_ico();
            if (!StringUtils.isEmpty(icoUrl)) {
                gameInfoHolder.itemMediaTestIv.setImageURI(icoUrl);
            }
            gameInfoHolder.itemMediaTestNameTx.setText(temp.getGame_name());
            String subTitle = temp.getGame_achievement_title();
            List<GamePrizesBean> listPrize = temp.getGame_prizes();
            if (listPrize!=null && listPrize.size()>0) {
                subTitle = listPrize.get(0).getTitle();
            }
            gameInfoHolder.itemMediaTestDecTx.setText(subTitle);

            if (viewBean.isEnd) {
                gameInfoHolder.itemBottomV.setVisibility(View.VISIBLE);
            } else {
                gameInfoHolder.itemBottomV.setVisibility(View.GONE);
            }

            downTemp.put(temp.getGame_id(), position+"");
            gameInfoHolder.itemDownloadBtn.initData(temp, 1);

            if (viewBean.getJumpModelType().equals("1001")) {
                gameInfoHolder.itemMediaTestRootRl.setBackgroundResource(R.drawable.media_test_google_bg);
            } else if (viewBean.getJumpModelType().equals("1002")) {
                gameInfoHolder.itemMediaTestRootRl.setBackgroundResource(R.drawable.media_test_apple_bg);
            } else if (viewBean.getJumpModelType().equals("1003")) {
                gameInfoHolder.itemMediaTestRootRl.setBackgroundResource(R.drawable.media_test_tag_bg);
            } else if (viewBean.getJumpModelType().equals("1004")) {
                gameInfoHolder.itemMediaTestRootRl.setBackgroundResource(R.drawable.media_test_taptap_bg);
            }

            gameInfoHolder.itemMediaTestRootRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentUtil.getInstance().goGamedetial(mContext, temp.getGame_id());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    private class MyListener implements View.OnClickListener {

        private Activity context;
        private JumpBaseBean tempBean;

        public MyListener(Activity context, JumpBaseBean temp) {
            this.context = context;
            this.tempBean = temp;
        }

        @Override
        public void onClick(View v) {
            IntentUtil.getInstance().intentTo(context, tempBean);
        }
    }

    public class TopHolder extends RecyclerView.ViewHolder {
        LinearLayout itemMediaTestTopLl;
        TextView itemMediaTestHistoryNameTv;
        TextView itemMediaTestNameTv;
        TextView itemMediaTestHistoryTv;

        public TopHolder(View view) {
            super(view);
            itemMediaTestTopLl = view.findViewById(R.id.itemMediaTestTopLl);
            itemMediaTestHistoryNameTv = view.findViewById(R.id.itemMediaTestHistoryNameTv);
            itemMediaTestNameTv = view.findViewById(R.id.itemMediaTestNameTv);
            itemMediaTestHistoryTv = view.findViewById(R.id.itemMediaTestHistoryTv);
        }
    }

    public class GameInfoHolder extends RecyclerView.ViewHolder {
        RelativeLayout itemMediaTestRootRl;
        SimpleDraweeView itemMediaTestIv;
        TextView itemMediaTestNameTx;
        TextView itemMediaTestDecTx;
        DownloadViewStrokeV2 itemDownloadBtn;
        View itemBottomV;

        public GameInfoHolder(View view) {
            super(view);
            itemMediaTestRootRl = view.findViewById(R.id.itemMediaTestRootRl);
            itemMediaTestIv = view.findViewById(R.id.itemMediaTestIv);
            itemMediaTestNameTx = view.findViewById(R.id.itemMediaTestNameTx);
            itemMediaTestDecTx = view.findViewById(R.id.itemMediaTestDecTx);
            itemDownloadBtn = view.findViewById(R.id.itemDownloadBtn);
            itemBottomV = view.findViewById(R.id.itemBottomV);
        }
    }
}
